<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <div class="seach-box">
        <div class="select-box-content">
          <div class="select-box-content-name">故障类型：</div>
          <el-select
            clearable
            v-model="hitType"
            placeholder="请选择"
            @change="selectHitchTypeCode(hitType)"
          >
            <el-option
              v-for="item in hitchTypeArr"
              :key="item.hitchTypeCode"
              :label="item.hitchTypeName"
              :value="item.hitchTypeCode"
            ></el-option>
          </el-select>
        </div>
        <div class="select-box-content">
          <div class="select-box-content-name">维修日期：</div>
          <el-date-picker
            v-model="repairDate"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            clearable
            @change="queryPageList()"
          />
        </div>
        <div class="operationBtn">
          <div class="screenBtn">
            <div class="btn" @click="changeSelect()">
              <i
                :class="activeName === '' ? 'el-icon-d-arrow-right' : 'el-icon-d-arrow-left'"
                class="arrow-down"
              ></i>
            </div>
          </div>
        </div>
        <el-collapse v-model="activeName" accordion @change="handleChange">
          <el-collapse-item name="1">
            <div v-if="equipmentArr.length < 15" class="select-box-content">
              <div class="select-box-content-name">设备名称：</div>
              <div
                class="select-box-content-all"
                @click="cancelSelect('equipmentCode')"
              >全部</div>
              <div
                v-for="(item, index) in equipmentArr"
                :key="index"
                class="select-box-content-list"
                :style="{
                  color:
                    equipmentCode === item.equipmentCode
                      ? '#54DEE2'
                      : '#C1CFD2',
                  fontWeight:
                    equipmentCode === item.equipmentCode ? '600' : '400'
                }"
                @click="selectEquipmentCode(item.equipmentCode)"
              >{{ item.equipmentName }}</div>
            </div>
            <div v-else class="select-box-content">
              <div class="select-box-content-name">设备名称：</div>
              <el-select
                v-model="equipmentCode"
                filterable
                clearable
                placeholder="请选择设备名称"
                @change="queryPageList()"
              >
                <el-option
                  v-for="item of equipmentArr"
                  :key="item.equipmentCode"
                  :label="item.equipmentName"
                  :value="item.equipmentCode"
                />
              </el-select>
            </div>
            <div class="select-box-content">
              <div
                v-if="topitemtypeId === '委内维修'"
                class="select-box-content-name"
              >维修人：</div>
              <div
                v-if="topitemtypeId === '委外维修'"
                class="select-box-content-name"
              >维修对接人：</div>
              <div
                class="select-box-content-all"
                @click="cancelSelect('repairUserId')"
              >全部</div>
              <div
                v-for="(item, index) in userList"
                :key="index"
                class="select-box-content-list"
                :style="{
                  color: repairUserId === item.username ? '#54DEE2' : '#C1CFD2',
                  fontWeight: repairUserId === item.username ? '600' : '400'
                }"
                @click="selectRepairUserId(item.username)"
              >{{ item.username }}</div>
            </div>
          </el-collapse-item>
        </el-collapse>
      </div>
    </template>

    <template v-slot:typenodeslot>
      <SelectButton
        :topitem-select-list="topitemSelectList"
        @topitemtype="topitemtype"
      />
    </template>
    <template v-slot:content>
      <e-table
        ref="table"
        :table-data="tableData"
        :table-columns="tableLabel"
        :showSelectColumn="false"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        :table-column-sum="topitemtypeId"
        rowKey="repairRecordCode"
        :expandRowKeys="expands"
        @sizeChange="changePageSize"
        @pageChange="changeCurrentPage"
        @tableUpdatePlus="mixTableUpdatePlus"
        @handleExpandChange="queryRepairHistory"
      >
        <template
          v-if="topitemtypeId === '委外维修'"
          slot="amount"
          slot-scope="scoped"
        >{{scoped.row.amount }}</template>

        <template
          slot="keyword"
          slot-scope="scoped"
          v-if="scoped.row.repairRecordCode"
        >
          <span class="table-btns-box" @click="queryKey(scoped.row)">
            <i class="el-icon-view"></i>
            <span>查看关键字</span>
          </span>
        </template>
        <template
          slot="expand"
          slot-scope="scoped"
          v-if="scoped.row.repairConsumeVOList&&scoped.row.repairConsumeVOList.length > 0"
        >
          <el-table
            v-loading="loading"
            :data="scoped.row.repairConsumeVOList"
            stripe
            border
            style="position: relative"
            height="100px"
            max-height="500px"
          >
            <el-table-column type="index" label="序号" width="50" />
            <el-table-column prop="deviceName" label="备件名称" />
            <el-table-column prop="num" label="数量" />
            <el-table-column prop="cost" label="费用" />
          </el-table>
        </template>
        <template
          slot="pictures"
          slot-scope="scoped"
          v-if="scoped.row.repairRecordCode"
        >
          <span class="table-btns-box" @click="queryPicture(scoped.row)">
            <i class="el-icon-view"></i>
            <span>查看图片</span>
          </span>
        </template>
        <template
          slot="videos"
          slot-scope="scoped"
          v-if="scoped.row.repairRecordCode"
        >
          <span class="table-btns-box" @click="queryVideo(scoped.row)">
            <i class="el-icon-view"></i>
            <span>查看视频</span>
          </span>
        </template>
        <template
          slot="repair_pictures"
          slot-scope="scoped"
          v-if="scoped.row.repairRecordCode"
        >
          <span class="table-btns-box" @click="queryRepairPicture(scoped.row)">
            <i class="el-icon-view"></i>
            <span>查看图片</span>
          </span>
        </template>
        <template
          slot="repair_videos"
          slot-scope="scoped"
          v-if="scoped.row.repairRecordCode"
        >
          <span class="table-btns-box" @click="queryRepairVideo(scoped.row)">
            <i class="el-icon-view"></i>
            <span>查看视频</span>
          </span>
        </template>
        <template
          slot="detail"
          slot-scope="scoped"
          v-if="scoped.row.repairRecordCode"
        >
          <span class="table-btns-box" @click="queryDetail(scoped.row)">
            <i class="el-icon-view"></i>
            <span>点击查看</span>
          </span>
        </template>
      </e-table>
    </template>
    <template slot="dialog">
      <AddEditDialog
        ref="addEditDialog"
        :topitemtype-id="topitemtypeId"
        :equipment-arr="equipmentArr"
        :hitch-type-arr="hitchTypeArr"
        :user-list="userList"
        :now-chance-stau="nowChanceStau"
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showAddEditDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <Video
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showVideoDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <Picture
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showPictureDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
      <Key
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showKeyDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
    </template>
  </BaseMainSlot>
</template>
<script>
import repairHistoryApi from "@/api/equip-repair-manage/repair-history";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import { mymixin } from "@/utils/mixin";
import SelectButton from "@/components/equipTypeSelectButton/selcetButton.vue";
import equipScrapRecordApi from "@/api/equip-manage/equip-scrap-record/index";
import AddEditDialog from "./components/add-edit-dialog.vue";
import Video from "@/components/detailDialog/video-dialog.vue";
import Picture from "@/components/detailDialog/picture-dialog.vue";
import Key from "@/components/detailDialog/key-dialog.vue";
import repairTypeApi from "@/api/equip-repair-manage/repair-type";
import userApi from "@/api/system-settings/user-manage/index";

export default {
  name: "RepairHistory",
  components: {
    BaseMainSlot,
    AddEditDialog,
    SelectButton,
    Video,
    Picture,
    Key,
  },
  mixins: [mymixin],
  data() {
    return {
      hitType: "",
      // 搜索条件:故障名称：
      hitchTypeCode: "",
      hitchTypeArr: [],
      //  搜索条件:设备名称：
      equipmentCode: "",
      equipmentArr: [],
      //  搜索条件:上报日期
      repairDate: "",
      // 搜索条件:任务名称：
      repairUserId: "",
      userList: [],
      // 委外维修,委内维修
      topitemtypeId: "委内维修",
      topitemSelectList: [
        { ids: "NATURE_IN", name: "委内维修", id: "委内维修" },
        { ids: "NATURE_OUT", name: "委外维修", id: "委外维修" },
      ],
      handUsername: "",
      expands: [], //只展开当前行,放入当前行id
      userList: [],
      // 表单展示项
      tableLabel: [
        {
          label: "设备名称",
          param: "equipmentName",
          selectValue: "equipmentCode", // Select 实际查询参数
          show: true,
          type: "Select",
          typeObj: {
            // 动态数据和静态数据
            list: [],
            listName: ["equipmentName", "equipmentCode"], // 动态数据和静态数据数组字段名
          },
        },
        {
          label: "故障类型名称",
          param: "hitchTypeName", // 表格显示参数
          selectValue: "hitchTypeCode", // Select 实际查询参数
          show: true,
          type: "Select", // input Select Checkbox date
          typeObj: {
            // 动态数据和静态数据
            list: [],
            listName: ["hitchTypeName", "hitchTypeCode"], // 动态数据和静态数据数组字段名
          },
        },
        {
          label: "维修人",
          param: "handUsername",
          selectValue: "userId", // Select 实际查询参数
          show: true,
          type: "Select", // input Select Checkbox date
          typeObj: {
            // 动态数据和静态数据
            list: [],
            listName: ["username", "userId"], // 动态数据和静态数据数组字段名
          },
          // width: "80",
        },
        {
          label: "维修日期",
          param: "repairDate",
          type: "date",
          show: true,
          width: 120,
        },
        {
          label: "维修结果",
          param: "taskStatus",
          show: true,
        },
        {
          label: "维修金额",
          param: "amount",
          slotName: "amount",
          notSearch: true,
          show: true,
          // width: 80,
        },
        {
          label: "损耗备件",
          param: "expand",
          slotName: "expand",
          show: true,
          notSearch: true,
          type: "expand",
          // width: 80,
        },
        {
          label: "故障关键字",
          param: "hitchKeywordNames",
          show: true,
          slotName: "keyword",
          width: 120,
          notSearch: true,
        },
        {
          label: "故障图片",
          param: "pictures",
          show: true,
          slotName: "pictures",
          notSearch: true,
          // width: 100,
        },
        {
          label: "故障视频",
          param: "videos",
          show: true,
          slotName: "videos",
          notSearch: true,
          // width: 100,
        },
        {
          label: "维修图片",
          param: "pictures",
          show: true,
          slotName: "repair_pictures",
          notSearch: true,
          // width: 100,
        },
        {
          label: "维修视频",
          param: "videos",
          show: true,
          slotName: "repair_videos",
          notSearch: true,
          // width: 100,
        },
        {
          label: "结果描述",
          param: "detail",
          show: true,
          slotName: "detail",
          notSearch: true,
          // width: 100,
        },
      ],
    };
  },
  watch: {},
  created() {
    this.queryEquipment();
    this.queryhitchTypeList();
    this.queryUserList();
    this.queryPageList();
  },
  mounted() {},
  methods: {
    //查询设备维修记录
    queryRepairHistory(row, expandedRows) {
      console.log(row, expandedRows);
      return false;
      let that = this;
      // let param = {
      //   page: 1,
      //   size: 1000,
      //   entity: {
      //     equipmentCode: row.equipmentCode,
      //   },
      // };
      // repairHistoryApi.queryPageList(param).then((res) => {
      //   this.repairHistoryList = res.data.content;
      //   // 每次只展开一行
      //   if (expandedRows.length) {
      //     that.expands = [];
      //     if (row) {
      //       that.expands.push(row.equipmentCode);
      //     }
      //   } else {
      //     that.expands = [];
      //   }
      // });
    },
    // 清空选项
    cancelSelect(data) {
      if (data === "hitchTypeCode") {
        this.hitchTypeCode = "";
      }
      if (data === "equipmentCode") {
        this.equipmentCode = "";
      }
      if (data === "repairUserId") {
        this.repairUserId = "";
      }
      this.queryPageList();
    },
    // 选择
    selectHitchTypeCode(id) {
      if (this.hitchTypeCode === id) {
        this.hitchTypeCode = "";
      } else {
        this.hitchTypeCode = id;
      }
      this.queryPageList();
    },
    // 选择
    selectEquipmentCode(id) {
      if (this.equipmentCode === id) {
        this.equipmentCode = "";
      } else {
        this.equipmentCode = id;
      }
      this.queryPageList();
    },
    // 选择
    selectRepairUserId(id) {
      console.log(id);
      if (this.repairUserId === id) {
        this.repairUserId = "";
      } else {
        this.repairUserId = id;
      }
      this.queryPageList();
    },
    // 条件查询未报废设备
    queryEquipment() {
      this.equipmentArr = [];
      equipScrapRecordApi.queryAllEqu().then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          for (var i in data) {
            this.equipmentArr.push({
              equipmentName: data[i].equipmentName,
              equipmentCode: data[i].equipmentCode,
            });
          }
          this.setTableColumnsPopover("equipmentName", this.equipmentArr);
        }
      });
    },
    // 条件查询故障类型
    queryhitchTypeList() {
      this.hitchTypeArr = [];
      repairTypeApi.findAll({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.hitchTypeArr = data;
          this.setTableColumnsPopover("hitchTypeName", this.hitchTypeArr);
        }
      });
    },
    // 查询可选择维修人
    queryUserList() {
      userApi.queryUserAll({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.userList = data;
          this.setTableColumnsPopover("handUsername", this.userList);
        }
      });
    },
    //表头 下拉框输入赋值
    setTableColumnsPopover(key, data) {
      let item = null;
      let index = 0;
      this.tableLabel.forEach((el, i) => {
        if (el.param === key && el.typeObj) {
          item = el;
          index = i;
          item.typeObj.list = data;
        }
      });
      item && this.$set(this.tableLabel, index, item);
    },
    getName(v) {
      var index = this.userList.findIndex((item) => item.id === v);
      if (index !== -1) {
        return this.userList[index]["username"];
      }
    },
    // 委内维修,委外维修
    topitemtype(e) {
      console.log(e);
      this.topitemtypeId = e;
      // 获取表格数据
      this.queryPageList();
    },
    queryPageList(e) {
      this.loading = true;
      // 获取数据
      let param = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          hitchTypeCode: this.hitchTypeCode,
          equipmentCode: this.equipmentCode,
          handUsername: this.repairUserId,
          repairDate: this.repairDate,
          repairNature: this.topitemtypeId,
          ...e,
        },
      };
      repairHistoryApi.queryPageList(param).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },

    // 查看详情
    queryDetail(row) {
      this.nowChanceStau = "showdetail";
      this.titleText = "查看详情";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showAddEditDialog = true;
    },
    // 查看关键字
    queryKey(row) {
      this.titleText = "关键字";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showKeyDialog = true;
    },
    // 查看视频
    queryVideo(row) {
      this.titleText = "故障视频";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showVideoDialog = true;
    },
    // 查看维修视频
    queryRepairVideo(row) {
      this.titleText = "维修视频";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showVideoDialog = true;
    },
    // 查看图片
    queryPicture(row) {
      this.titleText = "故障图片";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showPictureDialog = true;
    },
    //查看维修图片
    queryRepairPicture(row) {
      this.titleText = "维修图片";
      this.editObject = JSON.parse(JSON.stringify(row));
      this.showPictureDialog = true;
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-table {
  .el-icon {
    color: #fff;
  }
  .el-table__expanded-cell {
    background: #4affda00;
  }
}
::v-deep .el-collapse-item__header {
  height: 0px;
}
::v-deep .table-btns-box {
  margin-left: 0px;
}
</style>
